package com.ruoyi.ap.controller;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ap.domain.Village;
import com.ruoyi.ap.service.IVillageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 村庄Controller
 * 
 * @author HuaQP
 * @date 2023-09-11
 */
@RestController
@RequestMapping("/ap/village")
public class VillageController extends BaseController
{
    @Resource
    private IVillageService villageService;

    /**
     * 查询村庄列表
     */
    @PreAuthorize("@ss.hasPermi('ap:village:list')")
    @GetMapping("/list")
    public TableDataInfo list(Village village)
    {
        startPage();
        List<Village> list = villageService.selectVillageList(village);
        return getDataTable(list);
    }

    /**
     * 导出村庄列表
     */
    @PreAuthorize("@ss.hasPermi('ap:village:export')")
    @Log(title = "村庄", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Village village)
    {
        List<Village> list = villageService.selectVillageList(village);
        ExcelUtil<Village> util = new ExcelUtil<>(Village.class);
        util.exportExcel(response, list, "村庄数据");
    }

    /**
     * 获取村庄详细信息
     */
    @PreAuthorize("@ss.hasPermi('ap:village:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(villageService.selectVillageById(id));
    }

    /**
     * 新增村庄
     */
    @PreAuthorize("@ss.hasPermi('ap:village:add')")
    @Log(title = "村庄", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid Village village)
    {
        return toAjax(villageService.insertVillage(village));
    }

    /**
     * 修改村庄
     */
    @PreAuthorize("@ss.hasPermi('ap:village:edit')")
    @Log(title = "村庄", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Valid Village village)
    {
        return toAjax(villageService.updateVillage(village));
    }

    /**
     * 删除村庄
     */
    @PreAuthorize("@ss.hasPermi('ap:village:remove')")
    @Log(title = "村庄", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(villageService.deleteVillageByIds(ids));
    }
}
